﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BLL;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for messageBoardPreview.xaml
    /// </summary>
    public partial class messageBoardPreview : UserControl
    {
        private Message messageboardMessage;
        private const int MAX_PREVIEW_NUM_OF_WORDS = 25;

        public messageBoardPreview()
        {
            InitializeComponent();
        }

        public void SetMessageToPreview(Message messageToPreview)
        {
            messageboardMessage = messageToPreview;
            initPreview();
        }

        private void initPreview()
        {
            senderLabel.Content = "Author: " + GymWorkerManager.Instance.GetWorkerById(messageboardMessage.SenderId).FullName;
            subjectLabel.Content = "Subject: " + messageboardMessage.Subject;
            dateLabel.Content = messageboardMessage.CreateTime.ToString("dd/MM/yyyy HH:mm:ss");
            formatPreviewContent();
        }

        private void formatPreviewContent()
        {
            StringBuilder previewBuilder = new StringBuilder();
            char[] delimiters = {' '};
            string[] messageWords = messageboardMessage.Text.Split(delimiters);
            int substringLength = 0;
            string preview;

            if (messageWords.Length > MAX_PREVIEW_NUM_OF_WORDS)
            {

                for (int i = 0; i < MAX_PREVIEW_NUM_OF_WORDS; i++)
                {
                    substringLength += messageWords[i].Length + 1;
                }

                substringLength -= 4;

                preview = messageboardMessage.Text.Substring(0, substringLength);
                previewBuilder.Append(preview);
                previewBuilder.Append("...");
            }
            else
            {
                previewBuilder.Append(messageboardMessage.Text);
                previewBuilder.Append(" (No additional content)");
            }

            contentPreviewLabel.Content = previewBuilder.ToString();
        }

        private void viewMessageButton_Click(object sender, RoutedEventArgs e)
        {
            ViewMessageWindow viewMessageWindow = new ViewMessageWindow(messageboardMessage);
            viewMessageWindow.ShowDialog();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            updateContentPreviewLabelWidth();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            updateContentPreviewLabelWidth();
        }

        private void updateContentPreviewLabelWidth()
        {
            Thickness currentMargin = contentPreviewLabel.Margin;
            double rightMargin = viewMessageButton.Margin.Right;
            contentPreviewLabel.Width = previewGrid.ActualWidth - 2 * rightMargin;
        }
    }
}
